﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Truextend.CoffeShop.Presenter.Orders;
using System.Data;
using System.Web.UI.WebControls;

namespace Truextend.CoffeShop.Layouts.Truextend.CoffeShop
{
    public partial class ParticipateInOrder : LayoutsPageBase , IParticipateInOrder
    {
        static ParticipateInOrderPresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter = new ParticipateInOrderPresenter(this);

            if (!IsPostBack) {
                presenter.LoadOrder("Join");                
            }
        }

        protected void BtnRegisterPersonalOrder_Click(object sender, EventArgs e)
        {
            presenter.AddConsumption();
            Response.Redirect("~/CoffeeShop/SitePages/Home.aspx");
        }

        #region Implements ParticipateInOrder

        public int orderId
        {
            get
            {
                return int.Parse(Request.QueryString["orderId"].ToString());
            }
            set
            {                
            }
        }

        public string title
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                LblTitleContent.Text = value;
            }
        }

        public string restaurant
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                LblRestaurantContent.Text = value;
            }
        }        

        public int user_id
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.ID;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string user_name
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.LoginName;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public System.Data.DataTable options
        {
            get
            {
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("OptionID");
                dataTable.Columns.Add("Amount", typeof(int));
                dataTable.Columns.Add("Price", typeof(float));

                for (int i = 0; i < OptionsGridView.Rows.Count; i++)
                {
                    GridViewRow option_row = this.OptionsGridView.Rows[i];
                    CheckBox isChecked = option_row.FindControl("CheckBoxOption") as CheckBox;
                    TextBox amount = option_row.FindControl("TxBxAmount") as TextBox;
                    Label price = option_row.FindControl("LblPrice") as Label;

                    if (isChecked.Checked)
                    {
                        DataRow row = dataTable.NewRow();
                        row["OptionID"] = this.OptionsGridView.DataKeys[i].Value.ToString();
                        row["Amount"] = int.Parse(amount.Text);
                        row["Price"] = float.Parse(price.Text);
                        dataTable.Rows.Add(row);
                    }
                }

                return dataTable;
            }
            set
            {
                OptionsGridView.DataSource = value;
                OptionsGridView.Columns[0].Visible = false;
                OptionsGridView.DataBind();
            }
        }

        public float total_cost
        {
            get
            {
                return int.Parse(HideTotalPrice.Value.ToString());
            }
            set
            {
                HideTotalPrice.Value = value.ToString();
            }
        }

        #endregion

    }
}
